﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Text.RegularExpressions;

namespace YXShop.TemplateAction
{
    public partial class LabelParse
    {
        /// <summary>
        /// FS:LabelType=Product_Class标签处理函数
        /// </summary>
        /// <returns></returns>
        public string ProductClass()
        {
            string mystyle = this.Label_Inserted;
            string str_ShowMode = this.GetParamValue("YX:ShowMode");
            string str_ShowType = this.GetParamValue("YX:ShowType");
            string str_Showlevel = this.GetParamValue("YX:Showlevel");
            string str_CompositorType = this.GetParamValue("YX:CompositorType");
            string str_CompositorField = this.GetParamValue("YX:CompositorField");
            string str_StairShowRows = this.GetParamValue("YX:StairShowRows");
            int StairShowRows = 4;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_StairShowRows))
            {
                StairShowRows = Convert.ToInt32(str_StairShowRows);
            }

            string str_StairShowColumns = this.GetParamValue("YX:StairShowColumns");
            int StairShowColumns = 1;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_StairShowColumns))
            {
                StairShowColumns = Convert.ToInt32(str_StairShowColumns);
            }
           
            string str_TowShowRows = this.GetParamValue("YX:TowShowRows");
            int TowShowRows = 4;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_TowShowRows))
            {
                TowShowRows = Convert.ToInt32(str_TowShowRows);
            }

            string str_TowShowColumns = this.GetParamValue("YX:TowShowColumns");
            int TowShowColumns = 4;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_TowShowColumns))
            {
                TowShowColumns = Convert.ToInt32(str_TowShowColumns);
            }

            string str_StairCharacter = this.GetParamValue("YX:StairCharacter");
            int StairCharacter = 4;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_StairCharacter))
            {
                StairCharacter = Convert.ToInt32(str_StairCharacter);
            }

            string str_TowCharacter = this.GetParamValue("YX:TowCharacter");
            int TowCharacter = 4;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_TowCharacter))
            {
                TowCharacter = Convert.ToInt32(str_TowCharacter);
            }

            string str_IsOmit = this.GetParamValue("YX:WhetherOmit");
            string str_TempletLayout = this.GetParamValue("YX:TempletLayout");

            string orderSort = "";
            StringBuilder shtml = new StringBuilder();
            string Comd = " ";//条件
            YXShop.BLL.Product.Productclass bll = new YXShop.BLL.Product.Productclass();
            if (!string.IsNullOrEmpty(str_ShowMode))//显示模式
            {
                switch (str_ShowMode)
                {
                    case "NoneLimit":
                        Comd += " and fatherid=0 ";
                        break;
                    case "Parameter":
                        string id = ChangeHope.Common.StringHelper.InputTexts(HttpContext.Current.Request.QueryString["q_cid"]);
                        if (!string.IsNullOrEmpty(id))
                        {
                            if (ChangeHope.Common.ValidateHelper.IsNumber(id))
                            {
                                Comd += " and fatherid=" + id + "";
                            }
                        }
                        break;
                    case "SisterParameter":
                        string sisterid = ChangeHope.Common.StringHelper.InputTexts(HttpContext.Current.Request.QueryString["q_cid"]);
                        if (!string.IsNullOrEmpty(sisterid))
                        {

                            if (ChangeHope.Common.ValidateHelper.IsNumber(sisterid))
                            {
                                YXShop.Model.Product.Productclass model = bll.GetModelID(Convert.ToInt32(sisterid));
                                if (model != null)
                                {
                                    Comd += " and fatherid=" + model.Fatherid + "";
                                }
                            }
                        }
                        break;
                    case "ProID":
                        string ProID = HttpContext.Current.Request.QueryString["ProID"];
                        if (!string.IsNullOrEmpty(ProID) || ProID != null)
                        {
                            YXShop.BLL.Product.ProductInfo pd = new YXShop.BLL.Product.ProductInfo();
                            YXShop.Model.Product.ProductInfo ProductModel = pd.GetModel(Convert.ToInt32(ProID));
                            if (ProductModel != null)
                            {
                                if (ProductModel.pro_CID != "0")
                                {
                                    YXShop.Model.Product.Productclass datac = bll.GetModelID(Convert.ToInt32(ProductModel.pro_CID));
                                    if (datac != null)
                                    {
                                        if (datac.ID != 0)
                                        {
                                            Comd += " and fatherid=" + datac.ID + "";
                                        }
                                        else
                                        {
                                            Comd += " and fatherid=" + datac.ID + "";
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    case "ClassID":
                        string ClassID = ChangeHope.Common.StringHelper.InputTexts(HttpContext.Current.Request.QueryString["q_cid"]);
                        if (!string.IsNullOrEmpty(ClassID) && ClassID != null)
                        {
                            Comd += " and cid=" + ClassID + "";
                        }
                        break;
                    case "ProductClassID":
                        string productclassid = this.GetParamValue("YX:ProductClassID");
                        if (ChangeHope.Common.ValidateHelper.IsNumber(productclassid))
                        {
                            Comd += " and fatherid=" + productclassid + "";
                        }
                        break;

                }

            }
            if (!string.IsNullOrEmpty(str_ShowType) && str_ShowType == "1")
            {
                Comd += " and isrecommend=1";
            }
            //排序
            if (!string.IsNullOrEmpty(str_CompositorType) && str_CompositorType == "0")
            {
                if (!string.IsNullOrEmpty(str_CompositorField))
                {
                    orderSort = " [order by] " + str_CompositorField + " asc";
                }
            }
            else if (!string.IsNullOrEmpty(str_CompositorType) && str_CompositorType == "1")
            {
                if (!string.IsNullOrEmpty(str_CompositorField))
                {
                    orderSort = " [order by] " + str_CompositorField + " desc";
                }
            }
            int nowPageSize = StairShowRows * StairShowColumns;
            int TowPageSize = TowShowRows * TowShowColumns;
            if (!string.IsNullOrEmpty(mystyle))
            {
                string pattern = @"\{\#[\s\S]*?\#\}";
                string tempfinallyconent = mystyle;
                StringBuilder GetTemp = new StringBuilder();
                MatchCollection macths = Regex.Matches(tempfinallyconent, pattern);
                string Str_Tag = "";
                string temptag = "";
                foreach (Match macth in macths)
                {
                    temptag = macth.ToString();
                    int templength = temptag.ToString().IndexOf("{#") + 2;
                    int templength2 = temptag.ToString().IndexOf("#}");
                    if (temptag.ToString().Substring(templength, templength2 - templength).ToString() != null)
                    {
                        Str_Tag = temptag.ToString().Substring(templength, templength2 - templength);
                    }
                }
               
                ChangeHope.DataBase.DataByPage db = bll.GetList(orderSort, nowPageSize, Comd);

                if (db.DataReader != null)
                {
                    while (db.DataReader.Read())
                    {
                        StringBuilder towShtml = new StringBuilder();
                        if (str_Showlevel == "2")
                        {
                            //二级类型
                            if (Str_Tag != "")
                            {
                                ChangeHope.DataBase.DataByPage dbTow = bll.GetList(orderSort, TowPageSize, " and fatherid=" + db.DataReader["cid"].ToString() + "");
                                if (dbTow.DataReader != null)
                                {
                                    while (dbTow.DataReader.Read())
                                    {
                                       string secondContent = Str_Tag;
                                       int productCount=0;
                                       if (secondContent.Contains("[#ProductCount]"))
                                       {
                                           string classId = dbTow.DataReader["cid"].ToString();
                                           DataTable strdt = bll.GetBlurList(dbTow.DataReader["parentpath"].ToString() + ",");
                                           if (strdt.Rows.Count > 0)
                                           {
                                               for (int i = 0; i < strdt.Rows.Count; i++)
                                               {
                                                   classId += "," + strdt.Rows[i]["cid"].ToString();
                                               }
                                           }
                                           if (classId != "")
                                           {
                                               YXShop.BLL.Product.ProductInfo productbll = new YXShop.BLL.Product.ProductInfo();
                                               DataTable productdt = productbll.DTGetListWhere(" and cid in (" + classId + ")");
                                               productCount = productdt.Rows.Count;
                                           }
                                           secondContent = secondContent.Replace("[#ProductCount]", productCount.ToString());
                                       }
                                        
                                        if (secondContent.IndexOf("[#SecondName]") > -1)
                                        {
                                            string SecondNameContent = dbTow.DataReader["name"].ToString().Trim();
                                            if (ChangeHope.Common.ValidateHelper.IsNumber(StairCharacter.ToString()))
                                            {
                                                string str_Omit = "";
                                                if (str_IsOmit == "true")
                                                {
                                                    str_Omit = "...";
                                                }
                                                SecondNameContent = ChangeHope.Common.StringHelper.SubStringAndAppend(SecondNameContent, Convert.ToInt32(TowCharacter), str_Omit);
                                            }
                                            secondContent = secondContent.Replace("[#SecondName]", SecondNameContent);
                                            if (secondContent.IndexOf("[#URL]") > -1)
                                            {
                                                string url = Common.UrlStr("productlist", "q_cid=" + dbTow.DataReader["cid"].ToString() + "", "product/");
                                                secondContent = secondContent.Replace("[#URL]", url);
                                            }
                                            towShtml.Append(secondContent);
                                        }

                                    }
                                    dbTow.Dispose();
                                    dbTow = null;
                                }
                            }
                        }
                        //一级类型
                        string strContent = mystyle;
                        if (strContent.IndexOf("[#StairName]") > -1)
                        {
                            string StairNameContent = db.DataReader["name"].ToString().Trim();
                            if (ChangeHope.Common.ValidateHelper.IsNumber(TowCharacter.ToString()))
                            {
                                string str_Omit = "";
                                if (str_IsOmit == "true")
                                {
                                    str_Omit = "...";
                                }
                                StairNameContent = ChangeHope.Common.StringHelper.SubStringAndAppend(StairNameContent, Convert.ToInt32(StairCharacter), str_Omit);
                            }
                            strContent = strContent.Replace("[#StairName]", StairNameContent);
                        }
                        if (temptag != "")
                        {
                            if (towShtml.ToString() != string.Empty)
                            {
                                strContent = strContent.Replace(temptag, towShtml.ToString());
                            }
                            else
                            {
                                strContent = strContent.Replace(temptag, "");
                            }
                        }
                        if (strContent.Contains("[#ProductCount]"))
                        {
                            int productCount = 0;
                            string classId = db.DataReader["cid"].ToString();
                            DataTable strdt = bll.GetClassId(Convert.ToInt32(classId),db.DataReader["parentpath"].ToString() + "," + classId+",");
                            if (strdt.Rows.Count > 0)
                            {
                                for (int i = 0; i < strdt.Rows.Count; i++)
                                {
                                    classId += "," + strdt.Rows[i]["cid"].ToString();
                                }
                            }
                            if (classId != "")
                            {
                                YXShop.BLL.Product.ProductInfo productbll = new YXShop.BLL.Product.ProductInfo();
                                DataTable productdt = productbll.DTGetListWhere(" and cid in (" + classId + ")");
                                productCount = productdt.Rows.Count;
                            }
                            strContent = strContent.Replace("[#ProductCount]", productCount.ToString());
                        }
                        if (strContent.IndexOf("[#URL]") > -1)
                        {
                            string url = Common.UrlStr("productlist", "q_cid=" + db.DataReader["cid"].ToString() + "", "product/");
                            strContent = strContent.Replace("[#URL]", url);
                        }
                        shtml.Append(strContent);
                    }
                    db.Dispose();
                    db = null;
                }


            }
            if (shtml.ToString() == "")
            {
                shtml.Append("&nbsp;");
            }
            return shtml.ToString();
        }
    }
}
